package com.itbbfx.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itbbfx.entity.*;
import com.itbbfx.service.system.MenuService;
import com.itbbfx.service.system.RoleService;
import com.itbbfx.service.system.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/system")
public class SystemController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;

    // 用户管理

    /**
     * 查询用户（分页）
     * @param page
     * @param limit
     * @param user
     * @return
     */
    @RequestMapping("/selectAllUser")
    public Object selectAllUser(Integer page, Integer limit, User user){
        PageHelper.startPage(page,limit);
        PageInfo pageInfo = new PageInfo(userService.selectAllUser(user));
        return new DataGridView(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 查询登录名 是否重复
     * @param loginname
     * @return
     */
    @RequestMapping("/queryUserName")
    public int queryUserName(String loginname){
        return userService.queryUserName(loginname);
    }

    /**
     * 查询身份证号 是否重复
     * @param identity
     * @return
     */
    @RequestMapping("/queryUserIdentity")
    public int queryUserIdentity(String identity){
        return userService.queryUserIdentity(identity);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @RequestMapping("/addUser")
    public String addUser(User user){
        return userService.addUser(user);
    }


    @RequestMapping("/updateUser")
    public String updateUser(User user){
        return userService.updateUser(user);
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(int userid,HttpSession session){
        if(((User)session.getAttribute("user")).getUserid() == userid){
            return "不用删除当前登录用户！";
        }

        return userService.deleteUser(userid);
    }

    @RequestMapping("/resetUserPwd")
    public int resetUserPwd(int userid){
        return userService.resetUserPwd(userid);
    }

    @RequestMapping("/initUserRole")
    public DataGridView initUserRole(int userid){
        return userService.initUserRole(userid);
    }

    @RequestMapping("/saveUserRole")
    public int saveUserRole(UserRole userRole){
        return userService.saveUserRole(userRole);
    }

    // 角色管理
    @RequestMapping("/selectAllRole")
    public DataGridView selectAllRole(Integer page, Integer limit, Role role){
        PageHelper.startPage(page,limit);
        PageInfo pageInfo = new PageInfo(roleService.selectAllRole(role));
        return new DataGridView(pageInfo.getTotal(),pageInfo.getList());
    }

    @RequestMapping("/queryRoleName")
    public int queryRoleName(String rolename){
        return roleService.queryRoleName(rolename);
    }

    @RequestMapping("/addRole")
    public String addRole(Role role){
        return roleService.addRole(role);
    }

    @RequestMapping("/updateRole")
    public String updateRole(Role role){
        return roleService.updateRole(role);
    }

    @RequestMapping("/deleteRole")
    public String deleteRole(int roleid){
        return roleService.deleteRole(roleid);
    }

    /**
     * 分配角色权限
     * @param roleMenu
     * @return
     */
    @RequestMapping("/saveRoleMenu")
    public int saveRoleMenu(RoleMenu roleMenu){
        return roleService.saveRoleMenu(roleMenu);
    }

    @RequestMapping("/initRoleMenu")
    public DataGridView initRoleMenu(int roleid){
        return roleService.initRoleMenu(roleid);
    }

    @RequestMapping("/loadMenuMangerLeftTreeJson")
    public DataGridView loadMenuMangerLeftTreeJson(Menu menu){
        List<Menu> menus = menuService.queryAllMenu(menu);
        List<TreeNode> treeNodes = new ArrayList<>();

        for (Menu m : menus){
            treeNodes.add(new TreeNode(m.getId(),m.getPid(),m.getTitle(),m.getIcon(),
                    m.getHref(),m.getSpread()==true,m.getTarget()));
        }
        return new DataGridView(treeNodes);
    }

    @RequestMapping("/queryMenuAllList")
    public Object queryMenuAllList(Integer page, Integer limit, Menu menu){
        PageHelper.startPage(page,limit);
        List<Menu> menus = menuService.queryAllMenu(menu);
        PageInfo pageInfo = new PageInfo(menus);
        return new DataGridView(pageInfo.getTotal(),pageInfo.getList());
    }

    @RequestMapping("/checkMenuHasChildren")
    public Object checkMenuHasChildren(Integer id){
        return menuService.checkMenuHasChildren(id);
    }

    @RequestMapping("/deleteMenu")
    public int deleteMenu(Integer id){
        return menuService.deleteMenu(id);
    }

    @RequestMapping("/addMenu")
    public Object addMenu(Menu menu){
        return menuService.addMenu(menu);
    }

    @RequestMapping("/updateMenu")
    public Object updateMenu(Menu menu){
        return menuService.updateMenu(menu);
    }
}